import { observable, action, toJS } from 'mobx';
import { userType } from '../components/designer/form/dialog/addShare/constants';
import app from '../apis/app';
import { WeaTools } from 'ecCom';

export default class AddShareStore {
  @observable appid = '';
  @observable browsers = [];
  @observable hasInit = true;
  @observable settingDialogVisible = false;
  @observable addDialogVisible = false;
  @observable userIds = '';
  @observable userData = [];
  @observable selectGroupItems = [];
  @observable selectGroupIds = '';
  @observable departmentIds = '';
  @observable departmentItems = [];
  @observable subcompanyIds = '';
  @observable subcompanyItems = [];

  @action init = appid => {
    this.appid = appid;
    this.hasInit = false;

    this.getBrowsers();
  }

  @action getBrowsers = () => {
    app.getSubmitAuthInfo(this.appid).then(res => {
      if (res.type === 'SPECIFIED_USER') {
        
        this.browsers = res.user && res.user.filter(user => Boolean(user));
      }
      this.hasInit = true;
    })
  }

  @action delSubmitMember = (id, type) => {
    app.delSubmitMember({
      appid: this.appid,
      memberType: type,
      memberId: id
    }).then(() => {
      let data = toJS(this.browsers);

      data.forEach((p, i) => {
        if (p.id === id) {
          data.splice(i, 1)
        }
      });
      this.browsers = data;
    });
  }

  @action showSetGroupNoWriteDialog = () => {
    this.settingDialogVisible = true;
  }

  @action hideSetGroupNoWriteDialog = () => {
    this.settingDialogVisible = false;
  }

  @action showAddShareDialog = () => {
    this.addDialogVisible = true;
  }

  @action hideAddShareDialog = () => {
    this.addDialogVisible = false;
  }
  
  @action addShareMembers = (type, ids, datas) => {
    if (type === userType.USER) {
      this.userIds = ids;
      this.userData = datas;
    } else if (type === userType.CHATGROUP) {
      this.selectGroupItems = datas;
      this.selectGroupIds = ids;
    } else if (type === userType.DEPARTMENT) {
      this.departmentIds = ids;
      this.departmentItems = datas;
    } else if (type === userType.SUBCOMPANY) {
      this.subcompanyIds = ids;
      this.subcompanyItems = datas;
    }
  }

  @action onAddShareSure = (selectKey) => {
    let memberId = '';
    let memberType = '';

    switch(selectKey) {
      case 'person':
        memberId = this.userIds;
        memberType = userType.USER;
        break;
      case 'department':
        memberId = this.departmentIds;
        memberType = userType.DEPARTMENT;
        break;
      case 'subcompany':
        memberId = this.subcompanyIds;
        memberType = userType.SUBCOMPANY;
        break;
      case 'group':
        memberId = this.selectGroupIds;
        memberType = userType.CHATGROUP;
        break;
    } 

    const data = [{memberId, memberType}];

    app.addSubmitMember({
      appid: this.appid,
      data: JSON.stringify(data),
      sendForm: false
    }).then(() => {
      this.hideAddShareDialog();
      // 清空数据
      this.selectGroupItems = [];
      this.selectGroupIds = '';
      this.userIds = '';
      this.userData = [];
      this.departmentIds = '';
      this.departmentItems = [];
      this.subcompanyIds = '';
      this.subcompanyItems = [];
      this.init(this.appid);
    })
  }
}